//
//  BSBaseViewController.m
//  BiShang
//
//  Created by fancy on 2018/3/12.
//  Copyright © 2018年 bishang. All rights reserved.
//

#import "BSBaseViewController.h"

@interface BSBaseViewController ()

@end

@implementation BSBaseViewController

#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
    NSLog(@"%s dealloc", object_getClassName(self));
    NSLog(@"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
}

- (instancetype)init {
    return ([self initWithNibName:nil bundle:nil]);
}

- (instancetype)initWithSelfClassNameNib {
    return ([self initWithNibName:NSStringFromClass([self class]) bundle:[NSBundle mainBundle]]);
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self) {
        
    }
    return (self);
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActiveNotification:) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackgroundNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}
#pragma mark -

#pragma mark - public
- (void)keyboardWillShow {
    //root do nothing
}

- (void)keyboardWillHide {
    //root do nothing
}

- (void)keyboardDidChangeFrame {
    //root do nothing
}

- (void)applicationDidBecomeActive {
    //root do nothing
}

- (void)applicationDidEnterBackground {
    //root do nothing
}
#pragma mark -

#pragma mark - setter
- (void)setParameters:(id)parameters {
    
}
#pragma mark -

#pragma mark - orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0) {
    return (NO);
}

- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) {
    return (NO);
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) {
    return (UIInterfaceOrientationMaskPortrait);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0) {
    return (UIInterfaceOrientationPortrait);
}
#pragma mark -

#pragma mark - action
- (void)disableAdjustsScrollView {
    if([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)cleanEdgesForExtendedLayout {
    if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}

- (void)addKeyboardObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
}

- (void)removeKeyboardObserver {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
}

- (void)applicationDidBecomeActiveNotification:(NSNotification *)notification {
    [self applicationDidBecomeActive];
}

- (void)applicationDidEnterBackgroundNotification:(NSNotification *)notification {
    [self applicationDidEnterBackground];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    _keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //如果是搜狗等第三方输入法，keyboardWillShow通知会发送三次，第一次_keyboardFrame的高度是0，会使tableView往下抖一下再往上弹，判断高度大于0，再往controller发送键盘高度
    if (_keyboardFrame.size.height <= 0) {
        return;
    }
    _keyboardAnimateDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [self keyboardWillShow];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    _keyboardFrame = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    _keyboardAnimateDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [self keyboardWillHide];
}

- (void)keyboardDidChangeFrame:(NSNotification *)notification {
    _keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _keyboardAnimateDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [self keyboardDidChangeFrame];
}
#pragma mark -

@end
